#include "stdafx.h"
#include "DepthBuffer.h"
#include <assert.h>

DepthBuffer::DepthBuffer(ID3D11Device* device, unsigned int width, unsigned int height)
{
	D3D11_TEXTURE2D_DESC tex_desc;
	ZeroMemory(&tex_desc, sizeof(D3D11_TEXTURE2D_DESC));

	tex_desc.ArraySize = 1;
	tex_desc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_DEPTH_STENCIL;
	tex_desc.Format = DXGI_FORMAT_R32_TYPELESS;
	tex_desc.SampleDesc.Count = 1;
	tex_desc.Width = width;
	tex_desc.Height = height;
	
	device->CreateTexture2D(&tex_desc, nullptr, &depthBuffer);

	D3D11_DEPTH_STENCIL_VIEW_DESC dsv_desc;
	ZeroMemory(&dsv_desc, sizeof(D3D11_DEPTH_STENCIL_VIEW_DESC));

	dsv_desc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
	dsv_desc.Format = DXGI_FORMAT_D32_FLOAT;

	device->CreateDepthStencilView(depthBuffer, &dsv_desc, &depth_view);

	D3D11_SHADER_RESOURCE_VIEW_DESC srv_desc;
	ZeroMemory(&srv_desc, sizeof(D3D11_SHADER_RESOURCE_VIEW_DESC));

	srv_desc.Format = DXGI_FORMAT_R32_FLOAT;
	srv_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
	srv_desc.Texture2D.MostDetailedMip = 0;
	srv_desc.Texture2D.MipLevels = 1;

	device->CreateShaderResourceView(depthBuffer, &srv_desc, &srv);
}

/*virtual*/ DepthBuffer::~DepthBuffer(void)
{
	depth_view->Release();
	srv->Release();
	depthBuffer->Release();
}

/*virtual*/ void DepthBuffer::getData(void* data, unsigned int pixelStride) /*override*/
{
	assert("It's not posssible to get data from a depth buffer!");
}

/*virtual*/ void DepthBuffer::setData(void* data, unsigned int pixelStride) /*override*/
{
	assert("It's not possible to set the data of a depth buffer!");
}